local pattern = '[%z\1-\127\194-\244][\128-\191]*'

---- helper function
--local posrelat =
--function (pos, len)
--    if pos < 0 then
--        pos = len + pos + 1
--    end
--
--    return pos
--end

local utf8Fliter = {}
---- maps f over s's utf8 characters f can accept args: (visual_index, utf8_character, byte_index)
--utf8Fliter.map =
--function (s, f, no_subs)
--    local i = 0
--
--    if no_subs then
--        for b, e in s:gmatch('()' .. pattern .. '()') do
--            i = i + 1
--            local c = e - b
--            f(i, c, b)
--        end
--    else
--        for b, c in s:gmatch('()(' .. pattern .. ')') do
--            i = i + 1
--            f(i, c, b)
--        end
--    end
--end
--
---- THE REST
--
---- generator for the above -- to iterate over all utf8 chars
--utf8Fliter.chars =
--function (s, no_subs)
--    return coroutine.wrap(function () return utf8.map(s, coroutine.yield, no_subs) end)
--end
--
---- returns the number of characters in a UTF-8 string
--utf8Fliter.len =
--function (s)
--    -- count the number of non-continuing bytes
--    return select(2, s:gsub('[^\128-\193]', ''))
--end

-- replace all utf8 chars with mapping
utf8Fliter.replace =
function (s, map)
    return s:gsub(pattern, map)
end

return utf8Fliter